package org.csu.webjpetstoreback.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.csu.webjpetstoreback.config.CommonResponse;
import org.csu.webjpetstoreback.mapper.ProductMapper;
import org.csu.webjpetstoreback.model.Product;
import org.csu.webjpetstoreback.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Autowired
    public ProductMapper productsMapper;

    @Override
    public CommonResponse<List<Product>> GetProductsListById(String productId) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid",productId);
        List<Product> products = productsMapper.selectList(queryWrapper);
        if (products.size() > 0){
            return CommonResponse.BackForSuccess(products);
        }else{
            return CommonResponse.BackForError("当前不存在任何订单");
        }
    }



    @Override
    public CommonResponse<Product> PostProducts(Product product) {
        int result = productsMapper.insert(product);
        System.out.println(result);
        if (result ==1){
            return CommonResponse.BackForSuccess("新增product成功");
        }else{
            return CommonResponse.BackForError("新增product失败");
        }
    }

    @Override
    public CommonResponse<Product> PutProducts(Product product) {
        UpdateWrapper<Product> UpdateWrapper = new UpdateWrapper<>();
        int result = productsMapper.update(product,UpdateWrapper);
        if (result ==1){
            return CommonResponse.BackForSuccess("product修改成功");
        }else{
            return CommonResponse.BackForError("product修改失败");
        }
    }

    @Override
    public CommonResponse<Product> DeleteProducts(int productId) {

        int result = productsMapper.deleteById(productId);
        if (result == 1) {
            return CommonResponse.BackForSuccess("product删除成功!!!");
        } else {
            return CommonResponse.BackForError("product删除失败!!!");
        }
    }

    @Override
    public CommonResponse<List<Product>> GetProductsListByCategory(String categoryId){
            QueryWrapper<Product> queryWrapper =new QueryWrapper<>();
            queryWrapper.eq("category",categoryId);
            List<Product> productList =productsMapper.selectList(queryWrapper);
            return CommonResponse.BackForSuccess(productList);

        }

    @Override
    public CommonResponse<Product> GetProductByProductId(String ProductId) {
        QueryWrapper<Product> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("productid",ProductId);
        Product product=productsMapper.selectOne(queryWrapper);
        if (product != null){
            return CommonResponse.BackForSuccess(product);
        }else {
            return CommonResponse.BackForError("该product不存在");
        }
    }

    @Override
    public CommonResponse<List<Product>> GetProductListByKeywords(String Keywords) {
        QueryWrapper<Product> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("category",Keywords);
        List<Product> productList =productsMapper.selectList(queryWrapper);
        return CommonResponse.BackForSuccess(productList);
    }

}



